package com.tanhao.takeaway.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhao.takeaway.commom.R;
import com.tanhao.takeaway.entity.employee;
import com.tanhao.takeaway.service.employeeService;
import com.tanhao.takeaway.service.serviceImpl.employeeServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")

public class employeeCotroller {
    @Autowired
    private employeeService employeeServiceImp;

    @PostMapping("/login")
    public R<employee> login(HttpServletRequest httpServletRequest,@RequestBody employee e){
        String password=e.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(employee::getUsername, e.getUsername());
        employee  emp= employeeServiceImp.getOne(lambdaQueryWrapper);
        if(emp==null)
            return R.error("不存在用户");

        if(!emp.getPassword().equals(password))
            return R.error("密码错误");

        if(emp.getStatus()==0)
            return R.error("该账号不允许登录，已被封禁");

        httpServletRequest.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest httpServletRequest,@RequestBody employee e){
        e.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        e.setCreateTime(LocalDateTime.now());
//        e.setCreateUser((Long) httpServletRequest.getSession().getAttribute("employee"));
//        e.setUpdateTime(LocalDateTime.now());
//        e.setUpdateUser((Long) httpServletRequest.getSession().getAttribute("employee"));

        employeeServiceImp.save(e);
        return R.success("添加员工成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page p=new Page(page,pageSize);
        LambdaQueryWrapper <employee>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),employee::getName,name);
        lambdaQueryWrapper.orderByDesc(employee::getUpdateTime);
        employeeServiceImp.page(p,lambdaQueryWrapper);
        return R.success(p);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody employee e){
//        Long employId = (Long) request.getSession().getAttribute("employee");
//        e.setUpdateUser(employId);
//        e.setUpdateTime(LocalDateTime.now());
        employeeServiceImp.updateById(e);
        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R<employee> getById(@PathVariable Long id){
        employee e = employeeServiceImp.getById(id);
        if(e!=null)return R.success(e);
        return R.error("该员工不存在");
    }

}
